<?php
	session_start();
	
	if (!isset($_SESSION['login']) && !$_SESSION['login'] == true) {
		header ("Location: index.php");
	}
	
	include("sendmail.php");
	include ("config.php");
	
	$OwnerID = $_SESSION['userid'];
	$ProjectName = $_POST['Project'];
	$ContactName = $_POST['Contact'];
	$CustomerName = $_POST['Customer'];
	$OwnerRole = $_POST['OwnerRole'];
	$StatusID = 2;
	$Recommendation = $_POST['Recommendation'];
	$Problem = $_POST['Problem'];
	$Solution = $_POST['Solution'];
	$CustomerValue = $_POST['CustomerValue'];
	$NextStep = $_POST['NextStep'];
	$TimeFrame = $_POST['TimeFrame'];

	$stmt = $con->prepare('INSERT INTO `Innovation` (`OwnerID`, `Name`,`ContactName`, `CustomerName`, `OwnerRoleID`, `StatusID`, `Recommendation`, `Problem`, `Solution`, `CustomerValue`, `NextStepID`, `TimeFrameID`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
	$stmt->bind_param('isssiissssii', $OwnerID, $ProjectName, $ContactName, $CustomerName, $OwnerRole, $StatusID, $Recommendation, $Problem, $Solution, $CustomerValue, $NextStep, $TimeFrame);
	$stmt->execute();

	$InnoID = mysqli_insert_id($con);

	$stmt = $con->prepare('SELECT Innovation.Created, Innovation_NextStep.Value AS NextStep, Innovation_TimeFrame.Value AS TimeFrame FROM Innovation INNER JOIN Innovation_NextStep ON Innovation_NextStep.ID = Innovation.NextStepID INNER JOIN Innovation_TimeFrame ON Innovation_TimeFrame.ID = Innovation.TimeFrameID WHERE Innovation.ID = ?');
	$stmt->bind_param('i', $InnoID);
	$stmt->execute();
	$InnoInfoResult = $stmt->get_result();
	$InnoInfoRow = $InnoInfoResult->fetch_assoc();
	
	$stmt = $con->prepare('SELECT User.Name, User.Login, Company.Name AS CompanyName FROM User INNER JOIN Company ON Company.ID = User.CompanyID WHERE User.ID = (SELECT SupervisorID FROM User WHERE ID = ?)');
	$stmt->bind_param('i', $OwnerID);
	$stmt->execute();
	$UserResult = $stmt->get_result();
	$UserInfoRow = $UserResult->fetch_assoc();

	mysqli_close($con);
	
	$_SESSION['Popup'] = "NewRec";
	
	if(!empty($UserInfoRow))
		sendNewInnovation($UserInfoRow['Login'],$UserInfoRow['Name'],$InnoInfoRow['Created'],$UserInfoRow['CompanyName'],$CustomerName,$Recommendation,$Problem,$Solution,$CustomerValue);
	
	header ("Location: listinnovation.php");

?>